package com.kky.pp.common

import android.content.Context
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.kky.pp.popup.MemberUnitaryBuyDialog
import com.kky.pp.entity.BuyMemberPageEntity
import com.kky.pp.entity.BuyResultEnum
import com.kky.pp.pay.GooglePayUtils
import io.rong.imkit.entity.OrderCreateEntity
import com.kky.pp.callback.EmptySuccessCallBack

class MemberUnitaryBuy {
    fun openMemberUnitaryBuy(
        activity: Context,
        productCategory: Int,
        callBackListener: (reuslt: BuyResultEnum) -> Unit
    ) {
        MemberUnitaryBuyDialog(
            activity,
            productCategory,
            object : MemberUnitaryBuyDialog.MemberUnitaryBuyListener {
                override fun onListener(bean: BuyMemberPageEntity.ProductExt) {
                    OkHttpManager.instance.requestInterface(object :
                        OkHttpFromBoy {
                        override fun addBody(requestBody: OkHttpBodyEntity) {
                            requestBody.setPost(Constant.user_create_order_url)
                            requestBody.add("productCode", bean.productCode)
                            requestBody.add("productCategory", productCategory)
                        }

                    }, object : SDOkHttpResoutCallBack<OrderCreateEntity>() {
                        override fun onSuccess(entity: OrderCreateEntity) {
                            entity.data.benefitNum = bean.benefitNum
                            entity.data.productCategory = productCategory
                            GooglePayUtils.instance.start(
                                entity,
                                activity,
                                object : EmptySuccessCallBack {
                                    override fun OnSuccessListener() {
                                        callBackListener.invoke(BuyResultEnum.SUCCESS)
                                    }

                                })
                        }
                    })
                }

            })
    }
}